package com.uy.cuantoconocesdeuruguay.dialogs;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

import com.uy.cuantoconocesdeuruguay.R;
import com.uy.cuantoconocesdeuruguay.controllers.GoogleAnalyticsTracker;
import com.uy.cuantoconocesdeuruguay.datatypes.RandomImageEnvelope;
import com.uy.cuantoconocesdeuruguay.server.ImagesFetcher;

@SuppressLint("NewApi") 
public class EnlargeImageDialogUtils {
	private RandomImageEnvelope envelope;
	private static Drawable enlargedImage;

	private Activity activity;
	private ImageView pictureImageView;	
	public EnlargeImageDialogUtils(Activity activity) {
		this.activity = activity;
	}

	public void createEnlargeImagePopup(Drawable thumbnail, boolean isChallenge){
		View view = activity.getLayoutInflater().inflate(R.layout.enlarged_image, null);
		final ImageView image = (ImageView)view.findViewById(R.id.image);
		LinearLayout imageLayout = (LinearLayout)view.findViewById(R.id.imageLayout);
		
		LayoutParams fullLayoutParams = new LayoutParams(activity.getResources().getDisplayMetrics().widthPixels,activity.getResources().getDisplayMetrics().heightPixels);
		 
		view.setLayoutParams(fullLayoutParams);
	    imageLayout.getLayoutParams().height = activity.getResources().getDisplayMetrics().heightPixels;
		imageLayout.getLayoutParams().width = activity.getResources().getDisplayMetrics().widthPixels;
		image.setImageDrawable(thumbnail);
		AlertDialog.Builder builder = new AlertDialog.Builder(activity);
	    builder.setView(view);
	    builder.setCancelable(true);
	    final Dialog enlargedImagePopup = builder.create();
		enlargedImagePopup.show();	
		
		//SPINER LOADER
		View loaderView;
		if (isChallenge){
			loaderView = activity.getLayoutInflater().inflate(R.layout.challenge_loader_layout, null);
		}else{
			loaderView = activity.getLayoutInflater().inflate(R.layout.single_player_loader_layout, null);			
		}
		AlertDialog.Builder loader = new AlertDialog.Builder(activity);
		loader.setView(loaderView);
		loader.setCancelable(true);
	    final Dialog loaderDialog = loader.create();
	    
	    final AsyncTask<Void, Void, Void> asyncTask =  new AsyncTask<Void, Void, Void>() {
	    	@Override
	    	protected Void doInBackground(Void... arg0) {
	    		if (enlargedImage == null){
					Drawable drawable = new ImagesFetcher().fetchImage(activity, envelope.getImageURL(), false);
					enlargedImage = drawable;
				}
	    		return null;
	    	}
	    	@Override
	    	protected void onPostExecute(Void result) {
	    		try{
					loaderDialog.dismiss();
		    		image.setImageDrawable(enlargedImage);
		    		pictureImageView.setImageDrawable(enlargedImage);
	    		}catch (Exception e){
					GoogleAnalyticsTracker.trackException(activity, e);
	    			e.printStackTrace();
	    		}
	    	}	    	
		};
		if (android.os.Build.VERSION.SDK_INT > 10) { 
			asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
		}else{
			asyncTask.execute();
		}
		
	    loaderDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
	    loaderDialog.getWindow().clearFlags(android.view.WindowManager.LayoutParams.FLAG_DIM_BEHIND);
	    
		if (thumbnail.getBounds().height() < thumbnail.getBounds().width()){
			RotateAnimation rotateAnim = new RotateAnimation(0.0f, 90,
	            RotateAnimation.RELATIVE_TO_SELF, 0.5f,
	            RotateAnimation.RELATIVE_TO_SELF, 0.5f);
		    rotateAnim.setDuration(200);
		    rotateAnim.setFillAfter(true);
		    image.startAnimation(rotateAnim);
		    image.getLayoutParams().width = activity.getResources().getDisplayMetrics().heightPixels;//800;
		    image.getLayoutParams().height = activity.getResources().getDisplayMetrics().widthPixels;//480;
		}else{
			image.getLayoutParams().width = activity.getResources().getDisplayMetrics().widthPixels;//800;
			image.getLayoutParams().height = activity.getResources().getDisplayMetrics().heightPixels;//480;		
		}
		loaderDialog.setOnDismissListener(new OnDismissListener() {			
			@Override
			public void onDismiss(DialogInterface dialog) {
				asyncTask.cancel(true);
			}
		});
	    loaderDialog.show();	
	    image.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				enlargedImagePopup.dismiss();
			}
		});
	}
 
	public RandomImageEnvelope getEnvelope() {
		return envelope;
	}
	public void setEnvelope(RandomImageEnvelope envelope) {
		this.envelope = envelope;
	}
	
	public static void reset(){
		enlargedImage = null;
	}

	public void setImagePlaceholder(ImageView pictureImageView) {
		this.pictureImageView = pictureImageView;
	}
}
